package lab;

import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import progeny.Allele;
import progeny.Chromosome;
import progeny.Gene;
import progeny.Organism;
import progeny.Trait;

public class Punnet extends JFrame{
	
	private static final long serialVersionUID = -4552621818746943736L;
	
	public Punnet(Chromosome chromo){
		Allele[] list = chromo.getAlleles();
		
		int size = (int) Math.pow(2, list.length)+1;
		initGUI(initLife(list, size, 100000), list);
		
		this.setSize(300, 300);
		this.setVisible(true);
	}
	
	public void initGUI(int[][] pops, Allele[] list){
		int size = pops.length;
		JPanel pane = new JPanel();
		pane.setLayout(new GridLayout(size, size));
		this.setLayout(new GridLayout(1,1));
		this.add(pane);
		for (int i = 0; i < size; i++){
			for (int j = 0; j < size; j++){
				JTextArea jta = new JTextArea();
				jta.setEditable(false);
				String[] sides = PunnetHelper.getBorder(new Chromosome(list).getHalf().toString());
				if (i==0&&j==0){}
				else if (i==0) jta.setText(sides[j-1]);
				else if (j==0) jta.setText(sides[i-1]);
				else jta.setText(pops[i-1][j-1]+"");
				pane.add(jta);
			}
		}
	}
	public int[][] initLife(Allele[] list, int size, int iterations){
		int[][] pops = new int[size][size];
		Chromosome[] chroms = {new Chromosome(list)};
		Organism bob = new Organism(chroms);
		Organism alice = new Organism(chroms);
		ArrayList<Organism> organisms = new ArrayList<Organism>();
		for (int i = 0; i < iterations; i++)if (bob.canFuck(alice)) organisms.add(bob.fuck(alice));
		for (Organism o: organisms){
			String genotype = new StringBuilder(o.toString()).reverse().toString();
			String[] genes = genotype.split(",");
			int x = PunnetHelper.getPosition(genes, 0);
			int y = PunnetHelper.getPosition(genes, 1);
			pops[x][y]++;
		}
		return pops;
	}
	
	
	public static void main(String[] args){
		Gene A = new Gene(new Trait("A", true));
		Gene a = new Gene(new Trait("A", false));
		Gene B = new Gene(new Trait("B", true));
		Gene b = new Gene(new Trait("B", false));
		Gene C = new Gene(new Trait("C", true));
		Gene c = new Gene(new Trait("C", false));
		
		Allele[] genetics = {new Allele(A,a), new Allele(B,b), new Allele(C,c)};
		
		
		
		new Punnet(new Chromosome(genetics));
	}
}
